package util

import (
	"io/ioutil"
	"net/http"
	"time"
)

var client *http.Client

func init() {
	client = &http.Client{
		Transport: &http.Transport{
			MaxIdleConnsPerHost: 3,
			IdleConnTimeout:     time.Duration(30) * time.Second,
		},
		Timeout: time.Duration(30) * time.Second,
	}
}

func Request(uri, method string) ([]byte, error) {
	request, err := http.NewRequest(method, uri, nil)
	if err != nil {
		return nil, err
	}

	request.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "+
		"(KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36")
	resp, err := client.Do(request)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	return ioutil.ReadAll(resp.Body)
}
